package quartz1;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;

import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QuartzDemo {
	
	public void quartzrun() throws Exception{
		
		Logger log=LoggerFactory.getLogger(QuartzDemo.class);
		
		log.info("quartz start");
		
		log.info("构建SchedulerFactory");
		
		SchedulerFactory sf=new StdSchedulerFactory();
		
		log.info("构建Scheduler调度器");
		
		Scheduler scheduler=sf.getScheduler();
		
		Date runtime= DateBuilder.evenMinuteDate(new Date());
		
		log.info("初始化job和trigger");
		
		JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1","group1").build();
		
		CronTrigger trigger=(CronTrigger) TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * * * ?")).build();
		
		log.info("装载job和 trigger");
		
		scheduler.scheduleJob(job, trigger);
		
		log.info("启动quartz："+job.getKey()+"run at "+ runtime);
		 
		scheduler.start();
		
		/*
		try {
			Thread.sleep(65000L);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		log.info("关闭job");
		
		scheduler.shutdown();
		*/
	}
	
	public static void main(String[] args) {
		QuartzDemo demo=new QuartzDemo();
		
		try {
			
			demo.quartzrun();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	

	

}
